import {defineStore} from 'pinia';

// options API模式
export const usePersonStore = defineStore('person', {
    state: () => {
        return {
            name: '人类',
            count: 0,
        }
    },
    actions: {
        updateName(newName) {
            this.name = newName;
        }
    },
    getters: { // getters基本与Vue的计算属性一致
        getCount() {
            return `${this.count}人`;
        }
    }
})

// 对象形式
export const useTeacherStore = defineStore({
    id: 'teacher',
    state: () => {
        return {
            name: '老师',
            count: 0,
        }
    },
    actions: {
        updateName(newName) {
            this.name = newName;
        }
    },
    getters: {
        getCount() {
            return `${this.count}人`;
        }
    },
    persist: {
        enabled: true, // 开启缓存
        strategies: [
            {
                // key: 'infoStore', // 修改缓存的key
                storage: localStorage,    // 指定localStorage作为缓存
                paths: ['count']  // 只缓存name
            }
        ]
    },
})

// setup模式
import {computed, ref} from 'vue';

export const useStudentStore = defineStore('student', () => {
    const name = ref('学生');
    const count = ref(0);

    function updateName(newName) {
        name.value = newName;
    }

    const getCount = computed(() => `${count.value}人`);
    return {name, count, updateName, getCount}
})
